﻿using ColorWanted.util;
using ColorWanted.viewer;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;

namespace ColorWanted.clipboard
{
    static class ClipboardManager
    {
        public static bool IgnoreColor;
        public static readonly string DataRoot;


        static ClipboardManager()
        {
            DataRoot = Path.Combine(Glob.AppDataPath, "clipboard");
            if (!Directory.Exists(DataRoot))
            {
                Directory.CreateDirectory(DataRoot);
            }
        }

        /// <summary>
        /// 写文件并返回文件名
        /// </summary>
        /// <param name="text"></param>
        /// <param name="hashcode"></param>
        /// <returns></returns>
        public static string Write(string text, int hashcode)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return null;
            }
            var data = new ClipboardData()
            {
                Data = text,
                Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
            // 文件名为hashcode

            var dir = Path.Combine(DataRoot, DateTime.Now.ToString("yyyy/MM/dd"));
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var fullname = Path.Combine(dir, hashcode + ".board");

            File.WriteAllText(fullname, data.ToString(), Encoding.UTF8);
            return fullname;
        }

        public static ClipboardData Read(string fullname)
        {
            if (!File.Exists(fullname))
            {
                return null;
            }
            return ClipboardData.Parse(File.ReadAllText(fullname));
        }

        public static void Remove(string fullname)
        {
            if (File.Exists(fullname))
            {
                File.Delete(fullname);
            }
        }

        public static string[] Load(string path)
        {
            return Directory.GetFiles(path, "*.board", SearchOption.TopDirectoryOnly);
        }

        public static void Star(string fullname, string remark)
        {
            /// 保存方法:
            /// 1. 在 clipboard 目录下，新建一个 star 目录
            /// 2. 将当前的文件完整路径作为文件名，其内存储备注数据
            /// 3. 完整路径格式为: 2020#10#05#46454607.board
            Logger.Debug("Star data " + fullname);

            var starFileName = GetStarFileName(fullname);
            File.WriteAllText(starFileName, remark);
        }

        private static string GetStarFileName(string fullname)
        {
            // 获取 filename 的最后 4 部分: yyyy/mm/dd/filename
            var temp = fullname.Split(Path.DirectorySeparatorChar);
            var temp1 = temp.Skip(temp.Length - 4).Take(4);

            var startRoot = Path.Combine(DataRoot, "star");
            if (!Directory.Exists(startRoot))
            {
                Directory.CreateDirectory(startRoot);
            }

            var starFileName = Path.Combine(startRoot, string.Join("#", temp1));
            return starFileName;
        }

        internal static void UnStar(string fileName)
        {
            Logger.Debug("UnStar data " + fileName);

            var starFileName = GetStarFileName(fileName);
            if (File.Exists(starFileName))
            {
                File.Delete(starFileName);
            }
        }

        /// <summary>
        /// 检查是否收藏了此文件
        /// </summary>
        /// <param name="fullname"></param>
        /// <returns>收藏时，返回备注内容；否则返回 null</returns>
        public static string IsStarred(string fullname)
        {
            var starFileName = GetStarFileName(fullname);

            if (!File.Exists(starFileName))
            {
                return null;
            }

            return File.ReadAllText(starFileName);
        }
    }
}
